﻿using JNPF.Data.Util;
using JNPF.Utils;

namespace JNPF.Data
{
    public class DataFactory
    {
        public IDatabase Data(string dbConnString, DatabaseType dbType = DatabaseType.SqlServer)
        {
            return new AbstractDbAccessor(dbConnString, dbType);
        }
        public IDatabase Basic
        {
            get
            {
                var multiTenancy = ConfigurationKey.MultiTenancy;
                if (multiTenancy)
                {
                    var tenantProvider = TenantProvider.GetTenant;
                    var tenantDbType = tenantProvider.TenantDbType.ToDatabaseType();
                    var tenantDbConnectionString =string.Format(ConfigurationHelper.GetDbConnection("JNPF_DB_Default:ConnectionString"), tenantProvider.TenantDbConnectionString);
                    return this.Data(tenantDbConnectionString, tenantDbType);
                }
                else
                {
                    var dbName = ConfigurationHelper.GetDbConnection("JNPF_DB_Default:DBName");
                    var dbConnString = ConfigurationHelper.GetDbConnection("JNPF_DB_Default:ConnectionString");
                    dbConnString = string.Format(dbConnString, dbName);
                    var dbType = ConfigurationHelper.GetDbConnection("JNPF_DB_Default:ConnectionType").ToDatabaseType();
                    return this.Data(dbConnString, dbType);
                }
            }
        }
    }
}
